// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Axios from 'axios'
import Vuex from 'vuex'
import store from './store/store'

import MintUi, {Toast} from 'mint-ui'
import 'mint-ui/lib/style.css'
import Resource from 'vue-resource'
import Bus from './assets/bus'
Vue.use(Bus)
Vue.use(MintUi)


// 注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  const token = store.state.token
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (token) { // 通过vuex state获取当前的token是否存在
      next()
    } else {
      console.log('该页面需要登陆')
      Toast("请先登录")
      next({
        path: '/login'
      })
    }
  } else if(to.matched.length == 0){
    alert("错误路径")
    next({path:'/login'})
  }else{
    next()
  }
})



// 设置反向代理，前端请求默认发送到 http://localhost:8080/api
Axios.defaults.baseURL = 'http://localhost:8080/ssm_swagger_war_exploded/'
// Axios.defaults.baseURL = ''
// 全局注册，之后可在其他组件中通过 this.$axios 发送数据
Vue.prototype.$axios = Axios
Vue.config.productionTip = false
Vue.use(Vuex)
new Vue({
  el: '#app',
  router,
  store,
  Resource,
  components: { App },
  template: '<App/>'
})
